package com.ttbj.controller.order.carriage;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ttbj.constant.GenerateNumConstants;
import com.ttbj.controller.order.DefaultOrderController;
import com.ttbj.product.api.CostFacade;
import com.ttbj.product.api.OrderFacade;
import com.ttbj.product.api.OriginalPayFacade;
import com.ttbj.product.api.PayFacade;
import com.ttbj.product.api.WaybillFacade;
import com.ttbj.product.bean.dto.CargoDto;
import com.ttbj.product.bean.dto.OrderDto;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.Assert;
import com.ttbj.util.RequestUtils;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.TokenUtils;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/applet/carriage")
public class CarriageOrderController{

	@Autowired
	private OrderFacade orderFacade = null;

	@Autowired
	PayFacade payFacade = null;

	@Autowired
	CostFacade costFacade = null;

	@Autowired
	WaybillFacade waybillFacade = null;

	@Autowired
	OriginalPayFacade originalPayFacade = null;

	@RequestMapping("/new.do")
	@ResponseBody
	public ViewData carriageOrderNew(HttpServletRequest request, String openId, OrderDto orderDto, CargoDto cargo) throws Exception {
		UserDto userDto = (UserDto) request.getAttribute("user");
		Assert.assertFiledJsr(orderDto);
		Assert.assertFiledJsr(cargo);
		orderDto.openId(openId).setCargoDto(cargo);
		Float carri = orderDto.getOrderAllPay();
		if (null == carri || carri.equals(0f)) {
			carri = costFacade.getFreightCost(orderDto);// 重新计算运费
			if (null == carri || carri.equals(0f)) {
				ResultUtil.throwExcepion(ResultUtil.createFail("messages", 231), "计算运费失败");
			}
		}
		String orderNum = TokenUtils.getNo(GenerateNumConstants.ORDER);
		Integer orderDriverRequire = orderDto.getOrderDriverRequire();
		orderDto = orderFacade.createCarriageOrder(openId, orderDto.insTime()
				.no(orderNum)
				.wxUser(userDto.getId())
				.allPay(carri)
				.carriage(carri)
				.alreadTruck(0)
				.IsMove(1)
				.requireTruck(null == orderDriverRequire || orderDriverRequire.equals(0) ? 1 :orderDriverRequire)
				,cargo.insTime().no().orderNum(orderNum));
		Assert.assertNonNullObj(orderDto, 900, "生成订单失败");
		Map<String, String> paraMap = originalPayFacade.appletPay(RequestUtils.getRequestPath(request), carri, null, openId, String.valueOf(orderDto.getOrderId()));
		Assert.assertNonNullObj(paraMap, 232, "生成预支付参数失败");
		paraMap.put("fee", String.valueOf(carri));
		//originalPayFacade.payNotify(orderNum, openId, String.valueOf(orderDto.getOrderId()), UUID.randomUUID().toString(),1);
		return ViewModelUtil.buildSuccess("成功", paraMap);
	}

	 
	
	
	

}
